package com.anxpp.io.calculator.aio.client;

import java.util.Random;
import java.util.Scanner;

public class Client {
    private static String DEFAULT_HOST = "127.0.0.1";
    private static int DEFAULT_PORT = 12345;
    private static AsyncClientHandler clientHandle;
    public static void start(){
        start(DEFAULT_HOST,DEFAULT_PORT);
    }
    public static synchronized void start(String ip,int port){
        if(clientHandle!=null)
            return;
        clientHandle = new AsyncClientHandler(ip,port);
        new Thread(clientHandle,"Client").start();
    }
    //向服务器发送消息
    public static boolean sendMsg(String msg) throws Exception{
        if(msg.equals("q")) return false;
        clientHandle.sendMsg(msg);
        return true;
    }
//    @SuppressWarnings("resource")
    public static void main(String[] args) throws Exception{
        Client.start();
        Thread.sleep(1000);
//        System.out.println("请输入请求消息：");
//        Scanner scanner = new Scanner(System.in);
//        while(Client.sendMsg(scanner.nextLine()));
        final char operators[] = {'+', '-', '*', '/'};
        final Random random = new Random(System.currentTimeMillis());
        while(true){
            //随机产生算术表达式
            String expression = random.nextInt(10)+""+operators[random.nextInt(4)]+random.nextInt(10);
            if(!expression.contains("/0"))
                Client.sendMsg(expression);
//            try{
//                Thread.currentThread().sleep(random.nextInt(100));
//            }catch(InterruptedException e){
//                e.printStackTrace();
//            }
        }
    }

}


